<template>
  <div>
    <van-nav-bar title="地图找房" left-arrow @click-left="$router.back()" />
    <div id="container"></div>
  </div>
</template>

<script>
import { getMapcity } from '@/api/map'
export default {
  name: 'map-page',
  components: {},
  props: {},
  data() {
    return {}
  },
  async mounted() {
    const { BMapGL } = window
    const map = new BMapGL.Map('container')
    const obj = JSON.parse(localStorage.getItem('city'))
    const cityName = obj ? obj.label + '市' : '北京'
    map.centerAndZoom(cityName, 12)

    const res = await getMapcity({ id: obj.value })
    console.log(res)
    res.body.forEach((item) => {
      const point = new BMapGL.Point(item.coord.longitude, item.coord.latitude)
      const content = item.label + '<br>' + item.count
      const label = new BMapGL.Label(content, {
        // 创建文本标注
        position: point, // 设置标注的地理位置
        offset: new BMapGL.Size(10, 20) // 设置标注的偏移量
      })
      map.addOverlay(label) // 将标注添加到地图中
      label.setStyle({
        // 设置label的样式
        color: '#fff',
        fontSize: '14px',
        border: '2px solid #1E90FF',
        width: '60px',
        height: '60px',
        padding: '14px',
        borderRadius: '50%',
        background: 'rgba(12,181,106,.9)',
        textAlign: 'center'
      })
      label.addEventListener('click', async function () {
        const res = await getMapcity({ id: item.value })
        console.log(res)
        const point = new BMapGL.Point(
          item.coord.longitude,
          item.coord.latitude
        )
        map.centerAndZoom(point, 13)
        map.clearOverlays()
        res.body.forEach((item) => {
          const point = new BMapGL.Point(
            item.coord.longitude,
            item.coord.latitude
          )
          const content = item.label + '<br>' + item.count
          const label = new BMapGL.Label(content, {
            // 创建文本标注
            position: point, // 设置标注的地理位置
            offset: new BMapGL.Size(10, 20) // 设置标注的偏移量
          })
          map.addOverlay(label) // 将标注添加到地图中
          label.setStyle({
            // 设置label的样式
            color: '#fff',
            fontSize: '14px',
            border: '2px solid #1E90FF',
            width: '60px',
            height: '60px',
            padding: '14px',
            borderRadius: '50%',
            background: 'rgba(12,181,106,.9)',
            textAlign: 'center'
          })
          //   -----------------------
          label.addEventListener('click', async function () {
            const res = await getMapcity({ id: item.value })
            console.log(res)
            const point = new BMapGL.Point(
              item.coord.longitude,
              item.coord.latitude
            )
            map.centerAndZoom(point, 16)
            map.clearOverlays()
            res.body.forEach((item) => {
              const point = new BMapGL.Point(
                item.coord.longitude,
                item.coord.latitude
              )
              const content = item.label + '<br>' + item.count
              const label = new BMapGL.Label(content, {
                // 创建文本标注
                position: point, // 设置标注的地理位置
                offset: new BMapGL.Size(10, 20) // 设置标注的偏移量
              })
              map.addOverlay(label) // 将标注添加到地图中
              label.setStyle({
                // 设置label的样式
                color: '#fff',
                fontSize: '14px',
                border: '2px solid #1E90FF',
                width: '60px',
                height: '60px',
                padding: '14px',
                borderRadius: '50%',
                background: 'rgba(12,181,106,.9)',
                textAlign: 'center'
              })
            })
          })
        })
      })
    })
  },
  computed: {},
  watch: {},
  created() {},
  methods: {}
}
</script>

<style scoped lang="less">
#container {
  height: 93vh;
}
</style>
